﻿#NoEnv
#SingleInstance force

if (A_Args[1] = "c")
{
    content:=Clipboard
    content:=StrSplit(content, "`r`n")
    RunWaitMany(Generat_commands(content, A_Args[2], A_Args[3], A_Args[4], A_Args[5], A_Args[6], A_Args[7]))
    ExitApp
}
else if (A_Args[1] = "c no") ;不覆盖同名文件
    {
    content:=Clipboard
    content:=StrSplit(content, "`r`n")
    RunWaitMany(Generat_commands(content, A_Args[2], A_Args[3], A_Args[4], A_Args[5], A_Args[6], -1))
    ExitApp
    }
else if(A_Args[1] = "c del")
    {
    content:=Clipboard
    content:=StrSplit(content, "`r`n")
    RunWaitMany(Generat_commands(content, A_Args[2], A_Args[3], A_Args[4], A_Args[5], A_Args[6], A_Args[7]))
        for a,b in content
        {
            Sleep 1000
            FileRecycle, %b%
            if ErrorLevel
                MsgBox, 0x40010 , 源文件删除, %b%`r`n文件无法移入回收站
        }
        ExitApp
    }

#Include, Gdip_min.ahk

Menu, tray, NoStandard
Menu, Tray, Color, FFFFFF
Menu, Tray, Tip , PicConvert
Menu, tray, Add, 退出

gui, Color, FFFFFF, FFFFFF
gui, font, s13 , SimHei
gui, Add, Text, x90 y10 ,图片转换
gui, Add, Button, g选择图片  xp+100 yp-3 w20 h20 ,C
gui, Add, Button, g开始转换图片 xp+70 w30 h20 ,Go

gui, Add, Edit, -Wrap HScroll ReadOnly vall_pic x10 yp+30 w280 h150

gui, Add, Text, x10 yp+175 ,分辨率:
gui, Add, Edit, vsizew g提示 xp+110 yp w50 h20 Number
gui, Add, Text, yp xp+55, × 
gui, Add, Edit, vsizeh g提示 xp+22 yp w50 h20 Number
gui, Add, Button, g改变分辨率 xp+60 w20 h20 ,S

gui, Add, Text, x10 yp+35 ,输出格式:
gui, Add, Edit, voutType xp+110 yp w130 h20 Lowercase
gui, Add, Text, x10 yp+35 ,输出文件名:
gui, Add, Edit, vOutname xp+110 yp w130 h20
gui, Add, Text, x10 yp+35 ,输出文件夹:
gui, Add, Edit, vout_dir xp+110 yp w130 h20
gui, Add, Button, g选择文件夹 xp+140 w20 h20 ,C
gui, Add, Text, x10 yp+35 , 覆盖同名文件
gui, Add, Checkbox, Checked1 vifoverwrite xp+110 yp

gui, Add, Text, x370 ym , 拖入图片(可多张,但只显示第一张)
gui, Add, Picture, vpic x300 yp+20 w350 h350
gui, Show, AutoSize, PicConvert By~ 如初
return

退出:
GuiClose:
GuiEscape:
ExitApp

提示:
Tip("分辨率一栏留空`n则使用所有图片原来的分辨率")
return

开始转换图片:
gui, Submit, NoHide
if not outType
GuiControl, , outType, ico
Tip("开始转换图片")
RunWaitMany(Generat_commands(content, sizew, sizeh, outType, Outname, Out_dir, overwrite))
return

选择图片:
FileSelectFile, content, M3, %A_Desktop%, 选择图片(可按住Ctrl键进行多选)
if content
{
    ttt:=StrSplit(content, "`n")
    content:=""
    loop % ttt.Length() - 1
    {
        content.=ttt[1] "\" ttt[A_Index+1] "`r`n"
    }
    content:=Trim(content, "`r`n")
    gosub 解析图片
}
return

GuiDropFiles:
content:=StrReplace(A_GuiEvent, "`n", "`r`n")
解析图片:
gui, Cancel
GuiControl, , pic, 
GuiControl, , All_pic, % content
content:=StrSplit(content, "`r`n")
GuiControl, , pic, % content[1]
PToken := Gdip_Startup()
pBitmap:=Gdip_CreateBitmapFromFile(content[1])
Width:=Gdip_GetImageWidth(pBitmap)
Height:=Gdip_GetImageHeight(pBitmap)
Gdip_DisposeImage( pBitmap ) ;抛弃图片
Gdip_Shutdown( PToken)	;关闭项目

if (height >= Width)
{
    vs:=Width/height
    s_height:=350
    s_Width:=vs * s_height
}
else
{
    vs:=height/Width
    s_Width:=350
    s_height:=vs * s_Width
}

sizew:=Width
sizeh:=height
s_sizew:=Round(s_Width)
s_sizeh:=Round(s_height)

GuiControl,Move, pic ,w%s_sizew% h%s_sizeh%
GuiControl, , sizew, %sizew%
GuiControl, , sizeh, %sizeh%
gui, Show
return

选择文件夹:
FileSelectFolder, Out_dir, 
if Out_dir
    GuiControl, , out_dir, %out_dir%
return

改变分辨率:
    gui, Submit, NoHide
    if (sizew>0 and sizeh>0)
    {
        if (sizew >= sizeh)
        {
            vs:=sizeh/sizew
            s_sizew:=350
            s_sizeh:=vs * s_sizew
        }
        else
        {
            vs:=sizew/sizeh
            s_sizeh:=350
            s_sizew:=vs * s_sizeh
        }
        GuiControl, , pic, 
        GuiControl, Move,  pic, w%s_sizew% h%s_sizeh%
        GuiControl, , pic, % content[1]
    }
    else
    {
        Tip("请正确填写分辨率")
        Exit
    }
return


!q::
send ^s
Reload
return


F3::
Clipboard:=Generat_commands(["D:\Autohotkey\AHK集\我的脚本\测试\Starter\pic\saved_ico"])
return
;生成命令                      2          3             4             5           6             7
Generat_commands(content, sizew:=-1, sizeh:=-1, outType:="ico", Outname:="", Out_dir:="", overwrite:=1){
    
    if not sizew
        sizew:=-1
    if not sizeh
        sizeh:=-1
    if not overwrite =-1
        overwrite:=1
    if not outType
        outType:="ico"
    else if outType = jpg
        outType = jpeg
    
    SplitPath, A_ScriptDir, , , , , nconvert_Drive
    if content.Length() ;是数组
    {  
        commands:=""
        if overwrite = 1 ;覆盖同名文件
        {
            if (content.Length() > 1 and Outname) ;如果有多个文件同时处理，且有同样的名字---必须不覆盖
                overwrite:=""
            else
                overwrite:="-overwrite"
        }
        else
            overwrite:=""
        
        for a, b in content
        {
            SplitPath, b, , OutDir, ,OutNameNoExt
            outfile_dir:=OutDir
            
            if Outname
                OutNameNoExt:=Outname
            if Out_dir
                outfile_dir:=Out_dir
            ttt =
            (
%nconvert_Drive%
cd %A_ScriptDir%
nconvert -out %outType% %overwrite% -o %outfile_dir%\%OutNameNoExt%
            )
            commands.=ttt
            if (sizew >0 and sizeh >0) ;输出图像比例
                commands.=" " "-resize " sizew A_Space sizeh
            else if (outType ="ico" and sizew)
                commands.=" " "-resize " sizew A_Space sizew
            
            commands.=" " b "`r`n"
        }
    }
    else return 0
    return commands
}

RunWaitOne(command) { ;单条命令
    ; WshShell 对象: http://msdn.microsoft.com/en-us/library/aew9yb99
    shell := ComObjCreate("WScript.Shell")
    ; 通过 cmd.exe 执行单条命令
    exec := shell.Exec(ComSpec " /C " command)
    ; 读取并返回命令的输出
    return exec.StdOut.ReadAll()
}
 
RunWaitMany(commands) { ;多条命令 用换行符隔开
    shell := ComObjCreate("WScript.Shell")
    ; 打开 cmd.exe 禁用命令显示
    exec := shell.Exec(ComSpec " /Q /K echo off")
    ; 发送并执行命令,使用新行分隔
    exec.StdIn.WriteLine(commands "`nexit")  ; 保证执行完毕后退出!
    ; 读取并返回所有命令的输出
    return exec.StdOut.ReadAll()
}


